// uno.config.ts
import {
  Preset,
  defineConfig,
  presetAttributify,
  presetIcons,
  transformerDirectives,
  transformerVariantGroup,
  presetUno,
} from 'unocss'
import { presetRemRpx, transformerApplet, transformerAttributify } from 'unocss-applet'

const presets: Preset[] = []
// const isH5 = process.env?.UNI_PLATFORM === 'h5'
const isMp = process.env?.UNI_PLATFORM?.startsWith('mp') ?? false
if (isMp) {
  presets.push(presetRemRpx)
}
export default defineConfig({
  presets: [
    presetAttributify(),
    presetUno(),
    ...presets,
    // 支持图标，需要搭配图标库，eg: @iconify-json/carbon, 使用 `<button class="i-carbon-sun dark:i-carbon-moon" />`
    presetIcons({
      scale: 1.2,
      warn: true,
      extraProperties: {
        display: 'inline-block',
        'vertical-align': 'middle',
      },
    }),
  ],
  /**
   * 自定义快捷语句
   * @see https://github.com/unocss/unocss#shortcuts
   */
  shortcuts: [],
  transformers: [
    // 启用 @apply 功能
    transformerDirectives(),
    transformerVariantGroup(),
    // Don't change the following order
    transformerAttributify({
      // 解决与第三方框架样式冲突问题
      prefixedOnly: true,
      prefix: 'fg',
    }),
    transformerApplet(),
  ],
  rules: [
    [
      'p-safe',
      {
        padding:
          'env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left)',
      },
    ],
    ['pt-safe', { 'padding-top': 'env(safe-area-inset-top)' }],
    ['pb-safe', { 'padding-bottom': 'env(safe-area-inset-bottom)' }],
    [
      /^bg-\[(#[\d\D]{6})|(#[\d\D]{3})\]$/,
      ([, color]) => {
        return {
          'background-color': color,
        }
      },
    ],
    [
      /^text-\[(#[\d\D]{6})|(#[\d\D]{3})\]$/,
      ([, color]) => {
        return {
          color,
        }
      },
    ],
  ],
})
